package com.gombe.credit.app.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.gombe.credit.app.R
import com.gombe.credit.app.common.CommonActivity
import com.gombe.credit.app.configs.GombeCreditConfigs
import com.gombe.credit.app.databinding.ActGombeCreditPrivacyPolicyPageBinding
import com.gombe.credit.app.event.GombeCreditEventPointType
import com.gombe.credit.app.utils.GombeCreditTitleBarUtil
import com.gombe.credit.lib.adapter.GombeCreditOnSingleClickListener


/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:17
 * @Annotation：WebView
 */
class ActGombeCreditPrivacyPolicy : CommonActivity() {
    private lateinit var binding: ActGombeCreditPrivacyPolicyPageBinding

    companion object {
        private const val INTENT_LINK_URL = "linkUrl"
        private const val INTENT_LINK_TITLE = "linkTitle"
        fun start(context: Context, linkTitle: String, linkUrl: String) {
            val intent = Intent(context, ActGombeCreditPrivacyPolicy::class.java)
            intent.putExtra(INTENT_LINK_URL, linkUrl)
            intent.putExtra(INTENT_LINK_TITLE, linkTitle)
            context.startActivity(intent)
        }
    }

    override fun takeGombeCreditRootView(): View {
        binding = ActGombeCreditPrivacyPolicyPageBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initGombeCreditView() {
        setGombeCreditTitleBarVisibility(View.VISIBLE)
        setGombeCreditTextTitle(getWebViewTitle())
        setGombeCreditTextTitleColor(getColor(R.color.black))
        setGombeCreditRightBtnRes(R.mipmap.gombe_credit_image_customer_black_icon)
        setGombeCreditBackBtnRes(R.mipmap.gombe_credit_image_back_black_icon)
        GombeCreditTitleBarUtil.setStatusBarLightMode(this, true)
        setGombeCreditRootParentBackgroundColor(getColor(R.color.white))

        initWebViewSettings()
    }

    override fun initGombeCreditListener() {
        //联系客服
        rootViewBinding.rootCustomer.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                showCustomerDialog("${getWebViewTitle()} Page")
                addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.CUSTOMER,"${getWebViewTitle()} Page")
            }
        })
    }

    override fun takeGombeCreditData() {
        binding.webView.loadUrl(getWebViewUrl())
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.webView.setInitialScale(100)
        binding.webView.webChromeClient = mWebChromeClient
        binding.webView.webViewClient = mWebClient
        val webSettings = binding.webView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.webLoading.progress = newProgress
            if (newProgress == 100) {
                binding.webLoading.visibility = View.GONE
            } else {
                binding.webLoading.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?, ): Boolean {
            return shouldOverrideUrlLoading(view, request?.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    private fun getWebViewUrl(): String {
        if (intent.extras == null){
            return GombeCreditConfigs.GOMBECREDIT_PRIVACY_POLICY_SENEGAL_LINK
        }
        return intent.extras?.getString(INTENT_LINK_URL).toString()
    }

    private fun getWebViewTitle(): String {
        if (intent.extras == null){
            return getString(R.string.gombe_credit_privacy_policy_title)
        }
        return intent.extras?.getString(INTENT_LINK_TITLE).toString()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_PAGE.WEBVIEW_LINK_START,"${getWebViewTitle()} Page")
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_PAGE.WEBVIEW_LINK_END,"${getWebViewTitle()} Page")
    }
}